# -*- coding:utf-8 -*-
from common import assist_handle_mongo_data, extract_data, \
    del_dict_keys, extract_email, RabbitmqTool, \
    extract_phone, extract_regular_phone, extract_date, \
    change_money, date_change_time, extract_hanzi, DATABASE_CONF

from push_data.base import company_info_conf
from copy import deepcopy


def run(conf):
    rabbit_conf = conf.rabbit_conf
    rbt = RabbitmqTool(rabbit_conf)
    conf = conf.mongo_conf
    company_info_conf["connect"] = conf['connect']
    company_info_conf["db"] = conf['db']
    assist_handle_mongo_data(get_all_datas, company_info_conf,
                             args=[rbt, company_info_conf['map_data'], company_info_conf['contain_phone_list'],
                                   company_info_conf['contain_email_list']])


def get_all_datas(datas, rbt, map_data, contain_phone_list, contain_email_list):
    """
    对数据进行处理上传
    :param rbt:
    :param datas:
    :param map_data:
    :param contain_phone_list:
    :param contain_email_list:
    :return:
    """

    for data in datas:
        extra_dt = extract_data(data, map_data)
        phone_set = set()
        regular_phone_set = set()
        email_set = set()
        # 处理特殊特殊字段格式
        extra_dt["industry"] = extract_hanzi(extra_dt.get("industry", ""))
        extra_dt["register_money"] = change_money(extra_dt.get("register_money", ""))
        extra_dt["create_date"] = date_change_time(extract_date(extra_dt.get("create_date", "")))
        batch_join(phone_set, extract_phone, extra_dt, contain_phone_list)
        batch_join(regular_phone_set, extract_regular_phone, extra_dt, contain_phone_list)
        # 处理电话，固话，邮箱，进行上传
        batch_join(email_set, extract_email, extra_dt, contain_email_list)
        del_dict_keys(extra_dt, ["phone", "regular_phone", "email", "contact_information_type", "contact_information"])
        push_company_data(rbt, extra_dt, list(phone_set), list(regular_phone_set), list(email_set))


def push_company_data(rbt, datas, phone_set, regular_phone_set, email_set):
    """
    发送清洗后的数据，批次发送电话，固话，邮箱
    :param rbt:
    :param datas:
    :param phone_set:
    :param regular_phone_set:
    :param email_set:
    :return:
    """
    push_data = deepcopy(datas)
    info_datas = {
        'phone': len(phone_set),
        'regular_phone': len(regular_phone_set),
        'email': len(email_set),
    }
    lens = 0
    for key in info_datas:
        dt_len = info_datas[key]
        lens = lens if lens > dt_len else dt_len
    if lens:
        for le in range(lens):
            push_data["phone"] = phone_set[le] if info_datas["phone"] > le else ""
            push_data["regular_phone"] = regular_phone_set[le] if info_datas["regular_phone"] > le else ""
            push_data["email"] = email_set[le] if info_datas["email"] > le else ""
            print(push_data)
            # rbt.push_data(push_data)

    else:
        print(push_data)
        # rbt.push_data(push_data)


def batch_join(set_data, extra_func, datas, join_list):
    """
    批量提取合并
    :param set_data:
    :param extra_func:
    :param datas:
    :param join_list:
    :return:
    """
    for s in join_list:
        join_set_data(set_data, extra_func(datas.get(s, "")))


#
#
def join_set_data(set1, set2):
    """
    set2 数据合并到set1
    :param set1: type:set
    :param set2: 可遍历对象
    :return:
    """
    if set2:
        for s in set2:
            set1.add(s)


if __name__ == '__main__':
    conf = DATABASE_CONF["dev"]
    run(conf)
